//从浏览器拉取数据
var cityArr=JSON.parse(localStorage.getItem("cityArr"));//城市
var studentArr=JSON.parse(localStorage.getItem("studentArr"));//城市




//市校联动
//下拉框
var x = ["成都市", "绵阳", '雅安', '乐山', '内江'];
var y = [
    ["成都一中", "成都二中"],
    ["绵阳一中", "绵阳二中"],
    ["雅安一中", "雅安二中"],
    ["乐山一中", "乐山二中"],
    ["内江一中", "内江二中"],
];
var chose1 = document.getElementById("chose1");
var chose2 = document.getElementById("chose2");
chose1.length = x.length;
for (var i = 0; i < x.length; i++) {
    chose1.options[i].text = x[i];
}

chose2.length = y[0].length;
chose2.options[0].text = y[0][0];
function change1() {
    var index = chose1.selectedIndex;
    //    console.log(index);
    // chose2.length = y[index].length;
    for (var j = 0; j < y[index].length; j++) {
        chose2.options[j].text = y[index][j];
    };
}



var totalPage;//总页数
var currentPage=1;//当前页
var pageSize=6;//每页条数
var Flag=true;
addPageEvent()//分页
birdPage();//生成页码
displaystudent();//显示学生列表
addDelEvent();//删除学生
delForm();//清除表单
addModalvalue();//添加学生
viewStudents();//


//点击全选

    $(".checkA").on("click",".checkAll",function () {
        var check = $("tr input");
        console.log(check);
        var checked = this.checked;
        if (checked) {
            $(check).each(function (i) {
                check[i].checked = true;
            });
        } else {
            $(check).each(function (i) {
                check[i].checked = false;
                $(check).each(function (i) {
                    check[i].checked = false;
                })

            })
        }
    })

$(".checkA").on("click","tr input",function() {
    var check = $("tr input");
    check[0].checked = true;

    $(check).each(function (j) {
        if (check[j].checked == false) {
            check[0].checked = false;

        }
    })
})




//生成页面
function addPageEvent() {
    $("#divPage").on("click",".btn",function () {
        currentPage= Number($(this).text());
        displaystudent();

    })

}
function birdPage(){
    var studentArr = JSON.parse(localStorage.getItem("studentArr"));
    var totalSize=studentArr.length;
    totalPage=Math.ceil(totalSize/ pageSize);//总页数
    if(totalPage<currentPage){
        currentPage=totalPage;
    }
    $("#divPage").html("");
    for(var i=1;i<= totalPage;i++){
         $("#divPage").append(`<button class="btn btn-default">${i}</button>`)
    }
}
//删除事件
function addDelEvent() {
    // var studentArr=JSON.parse(localStorage.getItem("studentArr"));//浏览器调取学生数组
    $("#tbstu").on("click",".del",function () {
        var id=$(this).attr("data-id");

        var index = $(this).parent().parent()
        $(".delYes").click(function () {
            //删除当前的tr
            index.remove();
            //删除数组的数据
            delstudentbyid(id);
            birdPage();//重新生成页码
            displaystudent();
        })
    })
    // localStorage.setItem('studentArr',JSON.stringify(studentArr));
}
//通过id删除学生
function delstudentbyid(id) {
    var studentArr=JSON.parse(localStorage.getItem("studentArr"));//浏览器调取学生数组
    for(i=0;i<studentArr.length;i++){
        if(studentArr[i].stuNumber==id){
            studentArr.splice(i,1);
            break;
        }
    }
    localStorage.setItem('studentArr',JSON.stringify(studentArr));
}
//显示学生列表
function displaystudent() {
    $("#tbstu").html("");
    var studentArr=JSON.parse(localStorage.getItem("studentArr"));
    console.log(studentArr)
    var start=( currentPage-1)*pageSize;
    var end=start+pageSize;
    for(var i=start;i<end && i<studentArr.length;i++){
        $("#tbstu").append(`  <tr>
                                <td class="td1"><input type="checkbox" class="kuang"></td>
                                <td>${studentArr[i].stuNumber}</td>
                                <td>${studentArr[i].name}</td>
                                <td>${studentArr[i].sex}</td>
                                <td>${studentArr[i].classes}</td>
                                <td>${studentArr[i].state}</td>
                                <td>${studentArr[i].phone}</td>
                                <td>${studentArr[i].schoolId}</td>
                                <td>${studentArr[i].born}</td>
                                <td >
                                   <button class="btn btn-default view"  data-id="${studentArr[i].stuNumber}" data-toggle="modal" data-target="#myModal1">查看</button>
                                   <button class="btn btn-primary del"   data-id="${studentArr[i].stuNumber}" data-toggle="modal" data-target="#myModal" >删除</button>
                                   <button class="btn btn-success rewrite"  data-id="${studentArr[i].stuNumber}" data-toggle="modal" data-target="#myModal2" >修改</button>                            
                                </td>
                              </tr>`
        )
    }
}



//查看事件
function viewStudents(){
    $("#tbstu").on("click", ".view", function () {
        var studentArr=JSON.parse(localStorage.getItem("studentArr"));
        var a  =$(this).parent().parent().children()[1];
        // console.log($(a).text())
        var viewId = $(a).text();//查看的序号
        // console.log(viewId);
        for (var i = 0; i < studentArr.length; i++) {
            if (studentArr[i].stuNumber == viewId) {
                $(".viewXueHao").text(studentArr[i].stuNumber);
                $(".viewName").text(studentArr[i].name);
                $(".viewSex").text(studentArr[i].sex);
                $(".viewClasses").text(studentArr[i].classes);
                $(".viewMember").text(studentArr[i].state);
                $(".viewPhone").text(studentArr[i].phone);

                $(".viewXiaoHao").text(studentArr[i].schoolId);
                $(".viewBorn").text(studentArr[i].born);
                $(".viewAddress").text(studentArr[i].located);
                $(".viewFather").text(studentArr[i].father);
                $(".viewFatherTel").text(studentArr[i].fatherTel);
                $(".viewMother").text(studentArr[i].mother);
                $(".viewMotherTel").text(studentArr[i].motherTel);
                $(".viewBeiZhu").text(studentArr[i].beiZhu);

                break;

            }
        }
    })

}

//修改事件
$("#tbstu").on("click", ".rewrite", function () {
    var a  =$(this).parent().parent().children()[1];
    // console.log($(a).text())
    var viewId = $(a).text();//查看的序号
    // console.log(viewId);
    var studentArr=JSON.parse(localStorage.getItem("studentArr"));//浏览器调取学生数组
    $.each(studentArr, function (index, item) {
        if (item.stuNumber == viewId) {
            console.log()
            $(".adXueHao").val(this.stuNumber);
            $(".adName").val(this.name);
            $(".adClasses").val(this.classes);
            $(".adPhone").val(this.phone);
            $(".adXiaoHao").val(this.schoolId);

            $(".adBorn").val(this.born);
            $(".adAddress").val(this.located);
            $(".adFather").val(this.father);
            $(".adFatherTel").val(this.fatherTel);
            $(".adMother").val(this.mother);
            $(".addMotherTel").val(this.motherTel);
            $(".adBeiZhu").val(this.beiZhu);
        }
    })
    localStorage.setItem('studentArr',JSON.stringify(studentArr));
})

//修改确定事件
$('#btnOk').click(function () {
    var studentArr=JSON.parse(localStorage.getItem("studentArr"));//浏览器调取学生数组
    var a  =$(this).parent().parent().children()[1];
    // console.log($(a).text())
    var viewId = $(a).text();//查看的序号
    // console.log(viewId);
    var xuehao=$(".adXueHao").val();//输入学生学号

    var b=$(".adName").val();//输入学生姓名
    var c;//输入性别
    let maleX = $(".male");
    maleX.each(function () {
        if (this.selected) {
            c = $(this).text();
        }
    });

    var d=$(".adClasses").val();//shuru班级
    var e;//会员状态
    let memberX=$(".AdMember");
    memberX.each(function () {
            if(this.selected){
                e=$(this).text();
            }
    })
    var f=$(".adPhone").val();//输入手机号码
    var g=$(".adXiaoHao").val();//校号
    var h=$(".adBorn").val();//出生日期
    var i=$(".adAddress").val();//住址
    var j=$(".adFather").val();//父亲
    var k=$(".adFatherTel").val();//父亲电话
    var l=$(".adMother").val();//母亲
    var m=$(".addMotherTel").val();//母亲电话
    var n=$(".adBeiZhu").val();//备注


    for (var i = 0; i < studentArr.length; i++) {
        if (studentArr[i].stuNumber == xuehao){
            studentArr[i].name = b;
            studentArr[i].sex=c;
            studentArr[i].classes=d;
            studentArr[i].state=e;
            studentArr[i].phone=f;
            studentArr[i].schoolId=g;
            studentArr[i].born=h;
            studentArr[i].located=i;
            studentArr[i].father=j;
            studentArr[i].fatherTel=k;
            studentArr[i].mother=l;
            studentArr[i].motherTel=m;
            studentArr[i].beiZhu=n;


        }
    }
    localStorage.setItem('studentArr',JSON.stringify(studentArr));
    displaystudent();
})






//添加事件
function  addStudentEvent(){
    $(".adStu").click(function () {
           delForm();
           addModalvalue();

    })
}
//添加模态框里点击确定之后的取值函数
function addModalvalue() {
     $(".adYes").click(function (){
         var studentArr=JSON.parse(localStorage.getItem("studentArr"));
         var a=$(".addXueHao").val();//输入学生学号
         var b=$(".addName").val();//输入学生姓名
         var c;//输入性别
         let maleS = $(".updateMale");
         maleS.each(function () {
             if (this.selected) {
                 c = $(this).text();
             }
         });
         var d=$(".addClasses").val();//shuru班级
         var e;//会员状态
         let memberS=$(".AddMember");
             memberS.each(function () {
                 if(this.selected){
                     e=$(this).text();
                 }
             })
         var f=$(".addPhone").val();//输入手机号码
         var g=$(".addXiaoHao").val();//校号
         var h=$(".addBorn").val();//出生日期
         var i=$(".addAddress").val();//住址
         var j=$(".addFather").val();//父亲
         var k=$(".addFatherTel").val();//父亲电话
         var l=$(".addMother").val();//母亲
         var m=$(".adddMotherTel").val();//母亲电话
         var n=$(".addBeiZhu").val();//备注
         var obj={
             stuNumber: a,
             name: b,
             sex: c,
             classes: d,
             state: e,
             phone: f,
             schoolId: g,
             born:h,
             located:i,
             father:j,
             fatherTel:k,
             mother:l,
             motherTel:m,
             beiZhu:n,
         }
         studentArr.push(obj);
         localStorage.setItem('studentArr',JSON.stringify(studentArr));
         birdPage();
         displaystudent();
         delForm();
         viewStudents();






     })
}
//清除表单内容
 function delForm() {
       $("#adForm")[0].reset();
 }
